﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace UtilZ.Dotnet.Ex.Communication.Base
{
    /// <summary>
    /// 网络传输数据拆包基类
    /// </summary>
    public abstract class UnpackerAbs : IUnpacker
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public UnpackerAbs()
        {

        }


        /// <summary>
        /// 包输出事件
        /// </summary>
        public event EventHandler<DataOutputArgs> PackOutput;

        /// <summary>
        /// 触发包输出事件
        /// </summary>
        /// <param name="client">接收到数据的Client</param>
        /// <param name="args">参数</param>
        protected virtual void OnRaiseOnReceivedData(IClient client, DataOutputArgs args)
        {
            var handler = this.PackOutput;
            if (handler != null)
            {
                handler(client, args);
            }
        }


        /// <summary>
        /// 对数据进行拆包
        /// </summary>
        /// <param name="unpackerPara">分包参数</param>
        /// <returns>拆包成功的数据长度</returns>
        public int UnPackge(IUnpackerPara unpackerPara)
        {
            return this.PrimitiveUnPackge(unpackerPara);
        }

        /// <summary>
        /// 对数据进行拆包
        /// </summary>
        /// <param name="unpackerPara">分包参数</param>
        /// <returns>拆包成功的数据长度</returns>
        protected abstract int PrimitiveUnPackge(IUnpackerPara unpackerPara);



        #region IDisposable
        /// <summary>
        /// 获取当前对象是否已释放[true:已释放;false:未释放]
        /// </summary>
        public bool Disposabled
        {
            get
            {
                return _disposed;
            }
        }

        private bool _disposed = false;
        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            if (this._disposed)
            {
                return;
            }
            this._disposed = true;

            this.Dispose(true);
        }

        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing">标识[true:释放;false:重复释放]</param>
        protected virtual void Dispose(bool disposing)
        {

        }
        #endregion
    }
}
